
import 'package:flutter/cupertino.dart';
import 'package:flutter/material.dart';
import 'package:flutter_screenutil/flutter_screenutil.dart';
import 'package:gczn_hd/app/core/base/handle_mixin.dart';
import 'package:gczn_hd/app/core/values/App_img.dart';
import 'package:gczn_hd/app/core/values/app_colors.dart';
import 'package:gczn_hd/app/core/values/app_values.dart';
import 'package:gczn_hd/app/core/widget/gczn_card.dart';
import 'package:gczn_hd/app/modules/home/controllers/home_controller.dart';
import 'package:get/get.dart';

import '../../../core/values/text_styles.dart';

class UserListDrawer extends StatelessWidget with HandleMixin{
  final HomeController controller = Get.put(HomeController());

  Size buttonSize = Size(173.w, 60.h);
  final Size drawerSize = const Size(815,96);

  @override
  Widget build(BuildContext context) {
    double statusBarHeight = MediaQuery.of(context).padding.top;

    return Drawer(
      backgroundColor: AppColors.gcznDrawerBackground,
      width: drawerSize.width.w,
      child: Container(
        alignment: Alignment.topLeft,
        child: Column(
          children: [
            _buildHeader(statusBarHeight),
            _buildBody(),
            _buildFooter()
          ],
        ),
      )
    );
  }



  Widget _buildBody(){

    return Expanded(
      flex: 1,
      child: ListView(
        children: [1,2,3,4,5,6].map((item) => _buildUserList(item)).toList(),
      ),
    );
  }

  Widget _buildUserList(int idx){


    const double cardWidth = 735;

    return Center(
      child: Container(
        margin: EdgeInsets.only(bottom: AppValues.size37.h),
        width: cardWidth.w,
        child: GcznCard(
            child: Padding(
              padding:  EdgeInsets.only(left: AppValues.size38.w, right: AppValues.size38.w, top: AppValues.size28.h, bottom: AppValues.size28.h),
              child: Row(
                children: [
                  Expanded(
                    flex: 1,
                    child: Column(
                        crossAxisAlignment: CrossAxisAlignment.start,
                      children: [
                        Text('冠生', style: submitButtonText.copyWith(fontWeight: FontWeight.w700),),
                        SizedBox(height: AppValues.iconSize_14.h,),
                        Text('注塑1车间丨车间主管', style: submitButtonText.copyWith(color: submitButtonText.color?.withOpacity(AppValues.decimals2)))
                      ]
                    )
                  ),
                  if( idx == 1 )
                    Container(
                      child: Text("当前用户", style: submitButtonText,),
                    )
                  else
                    ElevatedButton(
                        onPressed: (){
                          //TODO   切换用户
                          toggleDialog();
                        },
                        style: btnStyle.copyWith(fixedSize:  WidgetStateProperty.all<Size>(buttonSize)),
                        child: Text("切换", style: submitButtonText,)
                    )
                ],
              ),
            )
        ),
      ),
    );
  }

  void toggleDialog(){

    Size dialogSize = const Size(715,455);
    Get.dialog(AlertDialog(
        contentPadding: EdgeInsets.zero,
        backgroundColor: AppColors.gcznPrimaryColorBg,
        content: Container(
          width: dialogSize.width.w,
          height: dialogSize.height.h,
          padding: EdgeInsets.all(AppValues.size48.h),
          decoration: BoxDecoration(
            borderRadius: BorderRadius.circular(AppValues.margin_12.r),
            color: AppColors.gcznPrimaryColorBg,
          ),
          child: Column(
            children: [
                Text("请输入所选用户[THQ]的登录密码", style: submitButtonText,),
                SizedBox(height: AppValues.size70.h,),

              _buildTextField(),
              SizedBox(height: AppValues.size70.h,),
              Row(
                mainAxisAlignment: MainAxisAlignment.center,
                children: [
                  ElevatedButton(
                      onPressed: (){
                        Get.back();
                      },
                      style: btnStyleCancle.copyWith(fixedSize:  WidgetStateProperty.all<Size>(buttonSize)),
                      child: Text("取消", style: submitButtonText,)
                  ),
                  SizedBox(width: AppValues.size46.w,),
                  ElevatedButton(
                      onPressed: (){
                        //TODO   切换用户
                        showToast("密码错喽");
                      },
                      style: btnStyle.copyWith(fixedSize:  WidgetStateProperty.all<Size>(buttonSize)),
                      child: Text("切换", style: submitButtonText,)
                  )
                ],
              )
            ],
          ),
        )
    ),);
  }

  Widget _buildTextField(){
    Size btnSize = const Size(490, 97);

    return  SizedBox(
      width: btnSize.width.h,
      height: btnSize.height.h,
      child: TextField(
        controller:  controller.passwordController,
        cursorColor: AppColors.colorWhite,
        obscureText: true,
        decoration: InputDecoration(
          filled: true,
          fillColor: AppColors.textFieldBackground,
          prefixIconConstraints: const BoxConstraints(),
          // isCollapsed: false,
          hintText: "请输入密码",
          hintStyle: textFieldHintStyle,
          border:  OutlineInputBorder(
              borderSide: const BorderSide(color: Color(0xFF4a5e76), width: 1),
              borderRadius: BorderRadius.all(Radius.circular(AppValues.margin_10.w))
          ),
          focusedBorder: const OutlineInputBorder(
            borderSide: BorderSide(color: AppColors.colorPrimary),
          ),
          contentPadding: const EdgeInsets.symmetric(horizontal: 20, vertical: 12),
        ),

      ),
    );
  }


  Widget _buildHeader(double height){

    return Container(
      width: drawerSize.width.w,
      height: (drawerSize.height+height).h,
      decoration: const BoxDecoration(
        gradient: LinearGradient(
          begin: Alignment.bottomCenter,
          end: Alignment.topCenter,
          colors: [
            Color.fromRGBO(18, 33, 47, 1),
            Color.fromRGBO(11, 23, 33, 1),
          ],
        ),
      ),
      child: Padding(
        padding: EdgeInsets.only(left: AppValues.size39.w, right: AppValues.size39.w, top: height.h),
        child: Row(
          children: [
            Expanded(
              flex: 1,
              child: Text('用户列表', style: whiteTextStyle.copyWith(fontSize: AppValues.extraLargeMargin.sp),),
            ),
            GestureDetector(
              onTap: (){
                Get.back();
              },
              child: ImageIcon(Image.asset(AppImg.drawerClose).image, size: AppValues.size23.w, color: AppColors.colorWhite,)
            )
          ],
        ),
      ),
    );

  }

  Widget _buildFooter(){

    const buttonSize = Size(735, 60);

    return Container(
        width: buttonSize.width.w,
        height: buttonSize.height.h,
        margin: EdgeInsets.only(bottom: AppValues.size42.h),
        child: ElevatedButton(
            onPressed: (){
              // showToast("退出喽");
              showConfirm(content: "您确定要退出当前帐号吗?", onConfirm: () async {
                await controller.logout();
              });
            },
            style:  ElevatedButton.styleFrom(
              foregroundColor: Colors.white,
              backgroundColor: AppColors.gcznPrimaryBlueColorBtn, // text color
              side: const BorderSide(
                color: AppColors.gcznPrimaryBlueColorBtn, // border-color
                width: 1, // border-width
              ),
              shape: RoundedRectangleBorder(
                borderRadius: BorderRadius.circular(AppValues.margin_12.r), // border-radius
              ),
              elevation: 0, // remove shadow if needed
            ),
            child: Text("退出登录", style: TextStyle(
                color: AppColors.colorWhite,
                fontSize: AppValues.margin_30.sp
            ))
        )
    );
  }

}